package com.taotao.rest.controller;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.rest.pojo.CartItem;
import com.taotao.rest.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 购物车Controller
 * <p>Title: CartController</p>
 * <p>Description: </p>
 * <p>Company: www.itcast.com</p>
 *
 * @author 入云龙
 * @version 1.0
 * @date 2015年9月15日下午3:12:59
 */
@Api(description = "购物车")
@RestController
public class CartController {

    @Autowired
    private CartService cartService;

    @ApiOperation("商品添加购物车")
    @PostMapping({"/cart/add/{itemId}", "/rest/cart/add/{itemId}"})
    public TaotaoResult addCartItem(@PathVariable Long itemId,
                                    @RequestParam(defaultValue = "1") Integer num,
                                    HttpServletRequest request, HttpServletResponse response) {
        return cartService.addCartItem(itemId, num, request, response);
//        return "redirect:/cart/success.html";
    }

    @RequestMapping("/success")
    public String showSuccess() {
        return "cartSuccess";
    }

    @ApiOperation("展示购物车")
    @GetMapping({"/cart/cart", "/rest/cart/cart", "/rest/cart/list"})
    public TaotaoResult showCart(HttpServletRequest request, HttpServletResponse response, Model model) {
        List<CartItem> list = cartService.getCartItemList(request, response);
        return TaotaoResult.ok(list);
//        model.addAttribute("cartList", list);
//        return "cart";
    }

    @ApiOperation("删除购物车商品")
    @DeleteMapping("/cart/delete/{itemId}")
    public TaotaoResult deleteCartItem(@PathVariable Long itemId, HttpServletRequest request, HttpServletResponse response) {
        return cartService.deleteCartItem(itemId, request, response);
//        return "redirect:/cart/cart.html";
    }

}
